﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    menucontroller.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-11-05  22:52
*/
#include "menucontroller.h"

#include <QJsonDocument>
#include <menuservice.h>
#include <menuvo.h>
#include <qjwtutils.h>
#include <user.h>
#include "responesutils.h"

MenuController::MenuController(QObject *parent) : HttpRequestHandler(parent)
{

}

MenuController::~MenuController()
{

}

void MenuController::service(HttpRequest &request, HttpResponse &response)
{
    QString path = request.getPath();
    QStringList pathList = path.split("/");
    if (pathList.size() < 3)
    {
        this->returnError(response);
        return;
    }
    QString method = pathList.value(3);
    QByteArray responseMsg;
    QString methodType = request.getMethod();

    if (methodType == "GET")
    {
        if (method == "all")
        {
            return getListBYAuth(request, response);
        }
        if (method == "list")
        {
            return getList(request, response);
        }
    }
    else if (methodType == "POST")
    {
        if (method == "save")
        {
            return saveOne(request, response);
        }
    }
    else if (methodType == "DELETE")
    {
        if (method == "delete")
        {
            return deleteOne(request, response);
        }
    }
    this->returnError(response);
}

void MenuController::getListBYAuth(HttpRequest &request, HttpResponse &response)
{
    QString authHeader = request.getHeader("Authorization");
    //权限找不到或者权限字符串头找不到
    if(authHeader.isEmpty() || authHeader.indexOf("Bearer ") == -1 || authHeader.split(" ").size() < 2){
        qDebug() << "权限找不到或者权限字符串头找不到";
        return ResponesUtils::unAuthorizedResponse(response);

    }
    QString token = authHeader.split(" ")[1];
    QJsonObject jsonObject= QJwtUtils::verifyAccessToken(token);
    User user;
    user.fromJsonObject(jsonObject);
    if(user.isEmpty()){
        return ResponesUtils::unAuthorizedResponse(response);
    }

    MenuService objectService;
    QList<MenuVO> list;
    if(!objectService.getVOListBYAuth(list,user.getRoles())){
          return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    QJsonArray jsonArray;
    for(MenuVO &item : list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void MenuController::getList(HttpRequest &request, HttpResponse &response)
{
    MenuService objectService;
    QList<Menu> list;
    if(!objectService.getList(list)){
          return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    QJsonArray jsonArray;
    for(Menu &item : list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void MenuController::saveOne(HttpRequest &request, HttpResponse &response)
{
    QByteArray body = request.getBody();
    QJsonParseError parseError;
    QJsonDocument  jsonDocument= QJsonDocument::fromJson(body, &parseError);

    if (parseError.error != QJsonParseError::NoError)
    {
        return ResponesUtils::erroResponse(response,"请求参数JSON结构异常!");
    }
    Menu object;
    object.fromJsonObject(jsonDocument.object().value("params").toObject());
    MenuService objectService;
    if(!objectService.saveOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"添加成功！");
}

void MenuController::deleteOne(HttpRequest &request, HttpResponse &response)
{
    uint64_t id = request.getParameter("id").toULongLong();
    if(id == 0 ){
        return ResponesUtils::erroResponse(response,"请指定删除ID！");
    }
    Menu object;
    object.setId(id);
    MenuService objectService;
    if(!objectService.deleteOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"删除成功！");
}
